package com.situ.student.dao.impl;

import com.situ.student.common.Global;
import com.situ.student.dao.ClassesDao;
import com.situ.student.dao.ClassesDao;
import com.situ.student.model.Classes;
import com.situ.student.model.Student;
import com.situ.student.model.Classes;
import com.situ.student.util.Pagination;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.List;

public class ClassesDaoImpl implements ClassesDao {
    private final RowMapper<Classes> rowMapper = new BeanPropertyRowMapper<>(Classes.class);
    public List<Classes> findAll(Pagination pagination, Classes classes){
        JdbcTemplate  jdbcTemplate = Global.getJdbcTemplate();
        List<Object> args = new ArrayList<>();
        String select = "select id,class_name,begin_time,end_time,major_id,student_cnt,status,description from t_class";
        StringBuilder where = new StringBuilder();
        where.append(" where 1=1");
        if (classes != null && StringUtils.hasText(classes.getClass_name())) {
            where.append(" and class_name like ?");
            args.add("%" + classes.getClass_name() + "%");
        }
        //查询记录总数
        String countSql = "select count(0) from (" + select + where + ") t";
        //记录总数
        Long count = jdbcTemplate.queryForObject(countSql, Long.class, args.toArray());
        pagination.setTotal(count);//设置总记录数

        //分页查询sql

        String limitSql = select + where + " limit ?,?";
        args.add(pagination.getOffset());
        args.add(pagination.getLimit());

        //执行查询操作
        List<Classes> list = jdbcTemplate.query(limitSql, rowMapper, args.toArray());
        return list;
    }

    public int deleteByIds(int id){
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        String sql = "delete from t_class where id = ?";
        int rows = jdbcTemplate.update(sql, id);
        return rows;
    }

    public int save(Classes classes){
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        String sql = "insert into t_class(class_name,begin_time,end_time,major_id,student_cnt,status,description) values(?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, classes.getClass_name(),classes.getBegin_time(),classes.getEnd_time(),classes.getMajor_id(),classes.getStudent_cnt(),classes.getStatus(),classes.getDescription());
        return rows;
    }
    public int update(Classes classes){
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        String sql = "update t_class set class_name = ?,begin_time = ?,end_time = ?,major_id =?,student_cnt=?,status=? ,description = ? where id = ?";
        int rows = jdbcTemplate.update(sql, classes.getClass_name(), classes.getBegin_time(), classes.getEnd_time(), classes.getMajor_id(), classes.getStudent_cnt(), classes.getStatus(), classes.getDescription(), classes.getId());
        return rows;
    }
    public Classes findById(Integer id){
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        String sql = "select id,class_name,begin_time,end_time,major_id,student_cnt,status,description from t_class where id = ?";
        Classes classes = jdbcTemplate.queryForObject(sql, rowMapper, id);
        return classes;
    }

}
